#!/usr/bin/env bash
set -ex

#Display environment
env

export TOP_DIR=`pwd`

#Clean up previous stuff
rm -rf build/ bin/

#Make directories
mkdir -p ../results/figures
mkdir -p ../results/tables

#Copy pregenerated outputs to output
mkdir -p ../results/temp
cp ../data/pregenerated_output/* ../results/temp/

#Build stuff
mkdir build
cd build && cmake -DCMAKE_BUILD_TYPE=Release .. && make -j 4 && make install

#Move back to /code directory
cd $TOP_DIR

#Install Python Mander
cd submodules/python-mander && /usr/bin/python3 setup.py install --user

cd $TOP_DIR

#Generate Koch snowflake data
./bin/koch.exe > ../results/temp/koch.csv
/usr/bin/python3 ./bin/koch_scores.py ../results/temp/koch.csv ../results/temp/koch_scores.csv

#Reproject data
/usr/bin/ogr2ogr -f "ESRI Shapefile" ../results/temp/cb_2015_us_cd114_500k_reproj.shp ../data/cb_2015_us_cd114_500k.shp -t_srs '+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +units=m +no_defs'
/usr/bin/ogr2ogr -f "ESRI Shapefile" ../results/temp/cb_2015_us_cd114_5m_reproj.shp   ../data/cb_2015_us_cd114_5m.shp   -t_srs '+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +units=m +no_defs'
/usr/bin/ogr2ogr -f "ESRI Shapefile" ../results/temp/cb_2015_us_cd114_20m_reproj.shp ../data/cb_2015_us_cd114_20m.shp   -t_srs '+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +units=m +no_defs'
#Reproject data
/usr/bin/ogr2ogr -f "ESRI Shapefile" ../results/temp/cb_2015_us_state_5m_reproj.shp   ../data/cb_2015_us_state_5m.shp   -t_srs '+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +units=m +no_defs'
/usr/bin/ogr2ogr -f "ESRI Shapefile" ../results/temp/cb_2015_us_state_500k_reproj.shp ../data/cb_2015_us_state_500k.shp -t_srs '+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +units=m +no_defs'
/usr/bin/ogr2ogr -f "ESRI Shapefile" ../results/temp/cb_2015_us_state_20m_reproj.shp  ../data/cb_2015_us_state_20m.shp  -t_srs '+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +units=m +no_defs'

#Augment shapefiles with score data
./bin/augmenter.exe ../results/temp/cb_2015_us_cd114_500k_reproj.shp ../results/temp/cb_2015_us_state_500k_reproj.shp ../results/temp/cb_2015_us_cd114_500k_scored.shp STATEFP
./bin/augmenter.exe ../results/temp/cb_2015_us_cd114_5m_reproj.shp   ../results/temp/cb_2015_us_state_5m_reproj.shp   ../results/temp/cb_2015_us_cd114_5m_scored.shp   STATEFP
./bin/augmenter.exe ../results/temp/cb_2015_us_cd114_20m_reproj.shp  ../results/temp/cb_2015_us_state_20m_reproj.shp  ../results/temp/cb_2015_us_cd114_20m_scored.shp  STATEFP

#Process data
/usr/bin/python3 ./bin/res_bounded.py            ../results/temp/cb_2015_us_cd114_500k_reproj.shp ../results/temp/cb_2015_us_state_500k_reproj.shp ../results/temp/cb_2015_us_cd114_500k_scored.shp STATEFP
/usr/bin/python3 ./bin/res_simp_together.py      ../data/cb_2015_us_cd114_500k.shp ../data/cb_2015_us_cd114_5m.shp ../data/cb_2015_us_cd114_20m.shp
/usr/bin/python3 ./bin/res_projections.py        ../data/cb_2015_us_cd114_500k.shp
/usr/bin/python3 ./bin/res_simp_indiv.py         ../data/cb_2015_us_cd114_500k.shp
/usr/bin/python3 ./bin/res_fig_gerrymandering.py ../data/cb_2015_us_cd114_500k.shp ../data/cb_2015_us_state_500k.shp

#Extract the score data
/usr/bin/dbfdump ../results/temp/cb_2015_us_cd114_500k_scored.shp  > ../results/temp/scores500.csv
#/usr/bin/dbfdump ../data/cb_2015_us_cd114_20m_scored.shp   > ../results/temp/scores20.csv #TODO: CUt
#/usr/bin/dbfdump ../data/cb_2015_us_cd114_5m_scored.shp    > ../results/temp/scores5.csv #TODO: CUT

#Generate the figures
Rscript ./bin/make_figs.R
cat ../results/temp/temp_table  | grep -E "tabular|document|package|&" > ../results/temp/table1.tex

#Compile table
pdflatex -output-directory ../results/temp/ ../results/temp/table1.tex
mv ../results/temp/table1.pdf ../results/tables/

#Generate district images
/usr/bin/python3 ./bin/districtplot.py 1205 1704 2103 2103 2103 2201 2201 2201 2402 2403 3701 3704 3712 4207 4833 4835

#Crop figures
ls ../results/temp/*koch*pdf | xargs -n 1 -I {} pdfcrop {} {}
ls ../results/temp/fig_evil*pdf | xargs -n 1 -I {} pdfcrop {} {}

#Generate LaTeX-based features
ls ./tex/* | xargs -n 1 pdflatex -output-directory ../results/temp/

#Juggle some files
mv ../results/temp/figure*pdf ../results/figures/
mv ../results/temp/figure*png ../results/figures/